//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.neurosky.thinkgear;

import android.util.Log;
import com.neurosky.thinkgear.DistanceArray;
import com.neurosky.thinkgear.EkgEpoch;
import com.neurosky.thinkgear.Matlab;
import com.neurosky.thinkgear.TGDevice;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class EkgTemplate {
    public String subjectName;
    public String fileName;
    public EkgEpoch[] templateArray;
    public int maxNumberOfTemplateElements = 100;
    public int lastTemplateIndex;
    public float score;
    public float meanDistance;

    public EkgTemplate(String var1) {
        this.subjectName = var1;
        this.templateArray = new EkgEpoch[this.maxNumberOfTemplateElements];
        this.lastTemplateIndex = 0;
    }

    public EkgTemplate() {
        this.subjectName = "";
        this.templateArray = new EkgEpoch[this.maxNumberOfTemplateElements];
        this.lastTemplateIndex = 0;
    }

    public boolean loadJson(String var1) {
        if(var1.length() == 0) {
            return false;
        } else {
            try {
                JSONObject var6 = (JSONObject)(new JSONTokener(var1)).nextValue();
                this.templateArray = new EkgEpoch[this.maxNumberOfTemplateElements];
                JSONArray var2 = var6.getJSONArray("templateArray");
                Log.v("TGDevice", "loadJson() a.length " + var2.length());

                for(int var3 = 0; var3 < var2.length(); this.lastTemplateIndex = var3++) {
                    this.templateArray[var3] = new EkgEpoch(var2.getJSONArray(var3));
                }

                this.subjectName = var6.getString("subjectName");
                return true;
            } catch (JSONException var4) {
                Log.d("EkgParameters", "JSON exception: " + var4.getLocalizedMessage());
                return false;
            } catch (ClassCastException var5) {
                return false;
            }
        }
    }

    public String toJSONString() {
        JSONObject var1 = new JSONObject();
        JSONArray var2 = new JSONArray();
        Log.v("TGDevice", "toJSONString() template array: " + this.templateArray.length);

        for(int var3 = 0; var3 < this.lastTemplateIndex; ++var3) {
            var2.put(this.templateArray[var3].toJSONArray());
        }

        Log.v("TGDevice", "toJsonString() arraylength:" + var2.length());

        try {
            var1.accumulate("subjectName", this.subjectName);
            var1.accumulate("templateArray", var2);
        } catch (JSONException var4) {
            var4.printStackTrace();
        }

        return var1.toString();
    }

    public float[][] getValues() {
        float[][] var1 = new float[this.lastTemplateIndex][this.templateArray[1].numberOfSamples];

        for(int var2 = 0; var2 < this.lastTemplateIndex; ++var2) {
            var1[var2] = this.templateArray[var2].data;
        }

        return var1;
    }

    public void addTemplate(int var1, float[] var2) {
        this.templateArray[this.lastTemplateIndex] = new EkgEpoch(var1, var2);
        ++this.lastTemplateIndex;
    }

    public EkgTemplate(String var1, float[][] var2) {
        this.lastTemplateIndex = 0;
        this.templateArray = new EkgEpoch[this.maxNumberOfTemplateElements];
        this.subjectName = var1;

        for(int var3 = 0; var3 < var2.length; ++var3) {
            this.addTemplate(var2[0].length, var2[var3]);
        }

        this.meanDistance = this.computeDistance(this).mean();
    }

    public void addData(EkgEpoch var1) {
        this.templateArray[this.lastTemplateIndex] = var1;
        ++this.lastTemplateIndex;
    }

    public void selectRows(int[] var1) {
        Arrays.sort(var1);

        for(int var2 = 0; var2 < var1.length; ++var2) {
            this.templateArray[var2] = this.templateArray[var1[var2]];
        }

        this.lastTemplateIndex = var1.length;
    }

    public void addTemplate(EkgEpoch var1) {
        this.templateArray[this.lastTemplateIndex] = var1;
        ++this.lastTemplateIndex;
    }

    public void rmTemplate(int var1) {
        int var10000 = this.lastTemplateIndex;

        for(var1 = var1; var1 < this.lastTemplateIndex - 1; ++var1) {
            this.copyTemplate(var1, var1 + 1);
        }

        --this.lastTemplateIndex;
    }

    public boolean equal(EkgTemplate var1) {
        if(this.lastTemplateIndex != var1.lastTemplateIndex) {
            return false;
        } else {
            for(int var2 = 0; var2 < this.lastTemplateIndex; ++var2) {
                if(this.templateArray[var2].data[0] != var1.templateArray[var2].data[0]) {
                    return false;
                }
            }

            return true;
        }
    }

    public static String buildStrongTemplate(String[] var0) {
        if(!TGDevice.ekgPersonalizationEnabled) {
            return null;
        } else {
            EkgTemplate[] var1 = new EkgTemplate[var0.length];

            for(int var2 = 0; var2 < var0.length; ++var2) {
                var1[var2] = new EkgTemplate();
                var1[var2].loadJson(var0[var2]);
            }

            return buildStrongTemplate(var1).toJSONString();
        }
    }

    public static EkgTemplate buildStrongTemplate(EkgTemplate[] var0) {
        if(!TGDevice.ekgPersonalizationEnabled) {
            return null;
        } else {
            int var1 = var0[0].lastTemplateIndex;
            EkgTemplate var2 = new EkgTemplate(var0[0].subjectName);
            if(var1 != var0.length && var0[var1] != null | var0[var1 - 1] == null) {
                //throw new Exception("There must be as many element in the" +
                       // " cell array as element in each template");
            } else {
                EkgTemplate var3 = new EkgTemplate("test");

                int var5;
                for(int var4 = 0; var4 < var1; ++var4) {
                    for(var5 = 0; var5 < var0[var4].lastTemplateIndex; ++var5) {
                        var3.addTemplate(var0[var4].templateArray[var5]);
                    }
                }

                DistanceArray var9 = var3.computeDistance(var3);

                for(var5 = 0; var5 < var1; ++var5) {
                    int[] var7 = Matlab.linspace(var5 * var1, (var5 + 1) * var1 - 1, var1);
                    int[] var6 = Matlab.setdiff(Matlab.linspace(0, var1 * var1 - 1, var1 * var1), var7);
                    float[] var8;
                    float[] var11;
                    Arrays.sort(var11 = (float[])(var8 = var9.subArray(var7, var6).colMean()).clone());
                    var7 = Matlab.sortIndices(var8, var11);
                    int var10 = (int)Math.ceil((double)var1 / 2.0D);
                    var2.addData(var0[var5].templateArray[var7[var10 - 1]]);
                }

                return var2;
            }
        }
        return null;
    }

    public DistanceArray computeDistance(EkgTemplate var1) {
        if(!TGDevice.ekgPersonalizationEnabled) {
            return null;
        } else {
            DistanceArray var2 = new DistanceArray(this.lastTemplateIndex, var1.lastTemplateIndex);
            boolean var3 = false;
            if(this.equal(var1)) {
                var3 = true;
            }

            for(int var4 = 0; var4 < this.lastTemplateIndex; ++var4) {
                int var5;
                if(var3) {
                    for(var5 = var4 + 1; var5 < var1.lastTemplateIndex; ++var5) {
                        try {
                            var2.array[var4][var5] = (float)Math.sqrt((double)this.templateArray[var4].subtract(var1.templateArray[var5]).square().mean());
                            var2.array[var5][var4] = var2.array[var4][var5];
                        } catch (ArrayIndexOutOfBoundsException var6) {
                            Log.v("TGDevice", "ind1: " + var4 + " ind2: " + var5);
                            Log.v("TGDevice", "templateArray Length: " + this.templateArray.length);
                        }
                    }
                } else {
                    for(var5 = 0; var5 < var1.lastTemplateIndex; ++var5) {
                        var2.array[var4][var5] = (float)Math.sqrt((double)this.templateArray[var4].subtract(var1.templateArray[var5]).square().mean());
                    }
                }
            }

            return var2;
        }
    }

    public void copyTemplate(int var1, int var2) {
        for(int var3 = 0; var3 < this.lastTemplateIndex; ++var3) {
            this.templateArray[var1] = this.templateArray[var2].clone();
        }

    }
}
